package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.dto.cent.sdata.RoomManageTypeDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.RoomManageType;
import com.xhwl.common.query.cent.sdata.RoomManageTypeQuery;
import com.xhwl.data.dao.IRoomManageTypeDao;
import com.xhwl.data.service.IRoomManageTypeService;
import com.xhwl.data.util.AutoCodeUtils;
import com.xhwl.data.util.CodePrefixConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Author yf
 * @Description:
 * @Date Create in 9:33 2022/06/14
 */
@Service
public class RoomManageTypeServiceImpl extends ServiceImpl<IRoomManageTypeDao, RoomManageType> implements IRoomManageTypeService {
    @Autowired
    private IRoomManageTypeDao roomManageTypeDao;

    @Override
    public ResultJson create(RoomManageTypeDTO dto) {
        if (null == dto.getName() || null == dto.getPrice()) {
            return ResultJson.fail("数据错误");
        }
        int insert = roomManageTypeDao.insert(dto);
        if (StringUtils.isBlank(dto.getCode())) {
            String code = AutoCodeUtils.codeFormat(CodePrefixConstant.ROOM_TYPE_PREFIX, dto.getId(), CodePrefixConstant.CODE_LENGTH);
            dto.setCode(code);
            roomManageTypeDao.updateById(dto);
        }
        return insert == 1 ? ResultJson.success("操作成功") : ResultJson.fail("操作失败");
    }

    @Override
    public ResultJson update(RoomManageTypeDTO dto) {
        int update = roomManageTypeDao.updateById(dto);
        return update == 1 ? ResultJson.success("操作成功") : ResultJson.fail("操作失败");
    }

    @Override
    public ResultJson delete(Integer id) {
        int delete = roomManageTypeDao.deleteById(id);
        return delete == 1 ? ResultJson.success("操作成功") : ResultJson.fail("操作失败");
    }

    @Override
    public Map<String, Object> page(RoomManageTypeQuery roomManageTypeQuery) {
        Integer page = roomManageTypeQuery.getPage();
        Integer size = roomManageTypeQuery.getPage();
        Page<RoomManageType> pageInfo = new Page<>(page,size);
        QueryWrapper<RoomManageType> wrapper = new QueryWrapper<>();
        wrapper.eq("enterprise_id",roomManageTypeQuery.getEnterpriseId());
        if (null != roomManageTypeQuery.getName()) {
            wrapper.eq("name", roomManageTypeQuery.getName());
        }
        if (null != roomManageTypeQuery.getCode()) {
            wrapper.eq("code", roomManageTypeQuery.getCode());
        }
        Page<RoomManageType> roomManageTypePage = roomManageTypeDao.selectPage(pageInfo, wrapper);
        roomManageTypeDao.selectPage(pageInfo, wrapper);
        List<RoomManageType> roomManageTypes = roomManageTypePage.getRecords();
        List<RoomManageTypeDTO> roomManageTypeDTOS = roomManageTypes.stream().map(x -> processBean(x)).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>(2);
        map.put("records", roomManageTypeDTOS);
        map.put("total", roomManageTypePage.getTotal());
        return map;
    }

    private RoomManageTypeDTO processBean(RoomManageType roomManageType) {
        RoomManageTypeDTO roomManageTypeDTO = new RoomManageTypeDTO();
        BeanUtils.copyProperties(roomManageType, roomManageTypeDTO);
        return roomManageTypeDTO;
    }
}
